<title>
Test for js memery
</title>
<head></head>

<script>
function test1(){

	var count = 0;
	var fibonacci = function(n){
		count++;
		return n < 2 ?  n : fibonacci(n-1) + fibonacci(n-2);
	};
	
	for(var i = 0; i<10; i++){
		console.log(i+':'+fibonacci(i));
	}
	
	console.log("count:"+count);
	
}


function test2(){

	var count = 0;
	
	var fibonacci = function(){
		var memo = [0, 1];
		var fib = function(n){
			count++;
			var result = memo[n];
			if(typeof result !== 'number'){
				result = fib(n-1) + fib(n-2);
				memo[n] = result;
			}
			return result;
		};
		return fib;
	};
	
	var fibb = new fibonacci();
	for(var i = 0; i<10; i++){
		console.log(i+':'+fibb(i));
	}
	
	console.log("count:"+count);
	
}

function test3(){

	var count = 0;
	
	var memoizer = function(memo, fundametal){
		var shell = function(n){
			count++;
			var result = memo[n];
			if(typeof result !== 'number'){
				result = fundametal(shell, n);
				memo[n] = result;
			}
			return result;
		};
		return shell;
	}	
	
	var fabonacci = memoizer([0, 1], function(shell,n){
		return shell(n-1)+shell(n-2);
	});

	
	for(var i = 0; i<10; i++){
		console.log(i+':'+fabonacci(i));
	}
	console.log("count:"+count);
}

</script>

<body>
<h1 onclick='test1()'>test1 click me!</h1>
<h1 onclick='test2()'>test2 click me!</h1>
<h1 onclick='test3()'>test3 click me!</h1>

</body>